#! C:\Perl\bin\perl.exe

# Copyright (c) 2010, College Center for Library Automation

# See the included Copyright.txt file for full license

use strict;
use warnings;

use LWP::UserAgent;
use HTTP::Request;
use HTTP::Request::Common;
use Date::Calc qw(Decode_Month Add_Delta_YM Add_Delta_Days Today Delta_Days);

use avsCommon;


dbsetup();

my ($content, $response, $request, $url);
my ($session, $collegeidsave);
my %stats;
my $stats;

# create user agent, set cookies
my $ua = new LWP::UserAgent(keep_alive=>1);
$ua->cookie_jar( {} );

# access vendor site to get logged in
getLogin();

# capture all the college id's in a hash for use during data extraction
my %college;
do {
	$college {$username} = $collegeid;
} while $sthlogin->fetch;

my ($runyear, $runmonth, $runday);
my @rundate = @begdate;
my $period;

my ($runendyear, $runendmonth, $runendday);

# loop through date range parameters
while (Delta_Days(@rundate, @enddate) > 0) {
	$collegeidsave = 0;

	($runyear, $runmonth, $runday) = @rundate;

	# loop through all the colleges for each data collection. put them back in collegeid order for summation routine
	foreach my $key (sort { $college{$a} <=> $college{$b} } ( keys(%college) )) {
		$username = $key;
		$collegeid = $college{$username};
		$collegeidsave = $collegeid if ! $collegeidsave;

		getStats ();
	}

	while ( (my $datasource, my $value) = each %stats )
	{
		my $session = $value->{sessions};
		my $search  = $value->{searches};
		print "$vendorid $collegeid $period $datasource $session $search\n";
		$sthstats->execute($vendorid, $collegeid, $datasource, $period, $session, 1)
			or die '#error: sthstats execute 1 failed';
		$sthstats->execute($vendorid, $collegeid, $datasource, $period, $search, 2 )
			or die '#error: sthstats execute 2 failed';
			
		$value->{searches} = $value->{sessions} = 0;
	}

	# advance the month
	@rundate = Add_Delta_YM(@rundate, 0, 1);
}

dbshutdown();

exit();

sub getLogin {
	#set up to get the menu page
	$url="http://infotrac.galegroup.com/itconfig/lincclin?id=$vendorusername&pass=$vendorpassword";
	
	#get the menu page
	$response = $ua->request(GET $url);
	if (!$response->is_success) {
		die "#error:$0: GET MENU $url : " . $response->status_line;
	};

	$content=$response->decoded_content;
	
	$content =~ /session\/(?<session>.*?)\/ctl/sm;
	$session = $+{session};
	
	# setup form for report request
	$url = "http://infotrac.galegroup.com/infotrac_config/session/$session/pg=mp!99&ul=2&un=1&uo=6";
	$response = $ua->request(GET $url);
	if (!$response->is_success) {
		die "#error:$0: GET MENU $url : " . $response->status_line;
	};

	$url = "http://infotrac.galegroup.com/infotrac_config/session/$session/cnt=mp!16";
	$response = $ua->request(GET $url);
	if (!$response->is_success) {
		die "#error:$0: GET MENU $url : " . $response->status_line;
	};
}

sub getStats {

	# setup dates to pass in url
	$period = myperiod($runyear, $runmonth);
	($runendyear, $runendmonth, $runendday) = 	Add_Delta_Days(Add_Delta_YM($runyear, $runmonth, 1, 0, 1), -1); # find last day of month by going to first day of next month and moving back one day

	my $sanitycheck = 0;
	my $year='';
	my $month='';
	my $data;

	$url = "http://infotrac.galegroup.com/infotrac_config/session/$session/pg=vo!344";

	# sanity check loop. occasionally Gale returns a different month than asked for.
	do {

		# execute the report request form
		$request = HTTP::Request->new(POST => $url);
		$request->content_type('application/x-www-form-urlencoded');
		$request->content("ui3=LOCATION&ui4=$username&ul=2&un=1&uo=6&uig=$runday&uia=$runmonth&uid=$runyear&uik=$runendday&uih=$runendmonth&uie=$runendyear&uiu=2&uif=HTML&uz=++Get+Report++");
		$response = $ua->request($request);

		# only check for data on success, otherwise, retry the request in the sanity loop.			
		if ($response->is_success) {
			$content=$response->decoded_content;

			# save contents for possible debugging
			savecontent('Gale', "$collegeid-$runyear-$runmonth", $content);

			# can we actually find the data?
			if ($content =~ /Date Period:<\/font>&nbsp;(?<month>\w*) 1 (?<year>\d{4}).*?<table.*?<\/tr>(?<data>.*?)<\/table>/s) {

				# check what month they ran for		
				$month = $+{month};
				$year  = $+{year};
				$data  = $+{data};
			}
			else {
				($month, $year, $data)  = '';
			}			
		}
	} while ( 
			( $runyear ne $year or $runmonth ne Decode_Month($month) )
			and $sanitycheck++ < 2);

	# after three attempts go on to something else
	if ($sanitycheck >= 2) {
		warn "#error: wrong data returned, please retry for $collegeid $runyear-$runmonth\n";
	}

	# occasionally no data returned
	elsif (! defined $data) {
		warn "#error: can't extract data from html. check file $collegeid-$runyear-$runmonth.txt";
	}

	else {

		if ($collegeidsave != $collegeid) {
			while ( (my $datasource, my $value) = each %stats )
			{
				my $session = $value->{sessions};
				my $search  = $value->{searches};
				print "$vendorid $collegeidsave $period $datasource $session $search\n";
				$sthstats->execute($vendorid, $collegeidsave, $datasource, $period, $session, 1)
					or die '#error: sthstats execute 1.2 failed';
				$sthstats->execute($vendorid, $collegeidsave, $datasource, $period, $search, 2 )
					or die '#error: sthstats execute 2.2 failed';
			}
			%stats = ();
			$stats = ();
			$collegeidsave = $collegeid;
		}


		# hopefullly have valid data, remove the html and find the stats
		$data =~ s/<.*?>/ /g;
		while ($data =~ /\n\s*(?<datasource>.*?) \(Total\).*?\n\s*(?<sessions>\d+)(.*?\n){3}.*?(?<searches>\d+)/mgc) {
			my $datasource = $+{datasource};
			my $session    = $+{sessions};
			my $search     = $+{searches};

			if (! exists $stats{$datasource} ) {
				$stats{$datasource}{sessions} = $session;
				$stats{$datasource}{searches} = $search;
			}

			else {
				$stats{$datasource}{sessions} += $session;
				$stats{$datasource}{searches} += $search;
			}
		}
	}
}
